package cn.hxth.cloud.provider.updater;

import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.Product;
import cn.hxth.cloud.api.domain.ProductInputOrder;
import cn.hxth.cloud.api.domain.ProductInputOrderItem;
import cn.hxth.cloud.api.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class InputProductUpdaterFactory extends AbstractProductUpdaterFactory<ProductInputOrder> {
    @Autowired
    public InputProductUpdaterFactory(DateProvider dateProvider, ProductMapper productMapper) {
        super(dateProvider, productMapper);
    }

    @Override
    protected List<Long> getProductIds(ProductInputOrder parameter) throws Exception {
        List<ProductInputOrderItem> items = parameter.getProductInputOrderItems() ;
        List<Long> ids = new ArrayList<>(items.size()) ;
        for(ProductInputOrderItem item:items){
            Long id = item.getPid() ;
            ids.add(id) ;

        }
        return ids;
    }

    @Override
    protected ProductUpdater createProductUpdater(List<Product> products, ProductInputOrder parameter) throws Exception {
        Map<Long,ProductInputOrderItem> map = new HashMap<>() ;
        for(ProductInputOrderItem item:parameter.getProductInputOrderItems()){
            map.put(item.getPid(),item) ;
        }
        return new InPutProductUpdater(products,productMapper,dateProvider,map) ;
    }
}
